package com.tech.service.business.impl;

import com.tech.service.business.BusinessService;
import com.tech.service.order.OrderService;
import com.tech.service.storage.StorageService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * Please add the follow VM arguments:
 * <pre>
 *     -Djava.net.preferIPv4Stack=true
 * </pre>
 */
@Slf4j
@DubboService
public class BusinessServiceImpl implements BusinessService {

    @DubboReference
    private StorageService storageService;
    @DubboReference
    private OrderService orderService;

    @Override
    @GlobalTransactional(timeoutMills = 300000)
    public void purchase(String userId, String commodityCode, int orderCount) {
        log.info("purchase begin ... xid: " + RootContext.getXID());
        storageService.deduct(commodityCode, orderCount);
        orderService.create(userId, commodityCode, orderCount);
        //throw new RuntimeException("xxx");

    }

}
